package com.bjpowernode.utils.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.config.context.ApplicationContextProvider;
import com.bjpowernode.service.TProductService;

import java.util.Map;

/**
 *意向产品
 */
public class IntentionProductConverter implements Converter<Integer> {

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        // 获取spring的bean
        TProductService tProductService = ApplicationContextProvider.getApplicationContext().getBean(TProductService.class);
        // 获取数据库中意向产品的 id - value一一对应值
        Map<String, Integer> integerStringMap = tProductService.selectAllNameAndId();
        // 获取excel数据
        String stringValue = cellData.getStringValue();
        // 查询数据对应的值 , 返回值
        return integerStringMap.get(stringValue);
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == 4) {
            return new WriteCellData<>("海豚");
        } else {
            return new WriteCellData<>("宋PLUS EV");
        }
        /*// 获取所有数据
        Map<Integer, String> integerStringMap = tProductService.selectAllIdAndName();
        // 查询value对应名称
        String s = integerStringMap.get(value);
        // new 并返回
        return new WriteCellData<>(s);*/
    }
}
